package com.ruoyi.system.service.impl;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.system.domain.IcEventInfo;
import com.ruoyi.system.domain.IcEventProcess;
import com.ruoyi.system.domain.vo.IcEventInfoVo;
import com.ruoyi.system.domain.vo.IcEventProcessVo;
import com.ruoyi.system.domain.vo.IcEventVo;
import com.ruoyi.system.service.IIcEventInfoService;
import com.ruoyi.system.service.IIcEventProcessService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.IcEventMapper;
import com.ruoyi.system.domain.IcEvent;
import com.ruoyi.system.service.IIcEventService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.util.CollectionUtils;

/**
 * 事件Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-04
 */
@Service
public class IcEventServiceImpl implements IIcEventService 
{
    @Autowired
    private IcEventMapper icEventMapper;

    @Autowired
    private IIcEventInfoService icEventInfoService;

    @Autowired
    private IIcEventProcessService icEventProcessService;

    /**
     * 查询事件
     * 
     * @param eId 事件主键
     * @return 事件
     */
    @Override
    public IcEvent selectIcEventByEId(Long eId)
    {
        return icEventMapper.selectIcEventByEId(eId);
    }

    /**
     * 查询事件列表
     * 
     * @param icEvent 事件
     * @return 事件
     */
    @Override
    public List<IcEvent> selectIcEventList(IcEvent icEvent)
    {
        return icEventMapper.selectIcEventList(icEvent);
    }

    /**
     * 新增事件
     * 
     * @param icEvent 事件
     * @return 结果
     */
    @Override
    public int insertIcEvent(IcEvent icEvent)
    {
        icEvent.setCreateTime(DateUtils.getNowDate());
        return icEventMapper.insertIcEvent(icEvent);
    }

    /**
     * 修改事件
     * 
     * @param icEvent 事件
     * @return 结果
     */
    @Override
    public int updateIcEvent(IcEvent icEvent)
    {
        icEvent.setUpdateTime(DateUtils.getNowDate());
        return icEventMapper.updateIcEvent(icEvent);
    }

    /**
     * 批量删除事件
     * 
     * @param eIds 需要删除的事件主键
     * @return 结果
     */
    @Override
    public int deleteIcEventByEIds(String eIds)
    {
        return icEventMapper.deleteIcEventByEIds(Convert.toStrArray(eIds));
    }

    /**
     * 删除事件信息
     * 
     * @param eId 事件主键
     * @return 结果
     */
    @Override
    public int deleteIcEventByEId(Long eId)
    {
        return icEventMapper.deleteIcEventByEId(eId);
    }

    @Override
    public List<IcEvent> selectIcEventByIuId(Long iuId) {
        return icEventMapper.selectIcEventByIuId(iuId);
    }

    @Override
    public List<IcEventVo> selectIcEventListVO(IcEvent icEvent)
    {
        List<IcEvent> events = icEventMapper.selectIcEventList(icEvent);
        if (CollectionUtils.isEmpty(events)) {
            return Collections.emptyList();
        }
        List<IcEventVo> collect = events.stream().map(event -> {
            IcEventVo icEventVo = new IcEventVo();
            BeanUtils.copyProperties(event, icEventVo);
            String typeName = DictUtils.getDictLabel("ic_event_type", event.geteType());
            icEventVo.seteTypeName(typeName);
            String auditName = DictUtils.getDictLabel("ic_event_audit", String.valueOf(event.geteAudit()));
            icEventVo.seteAuditName(auditName);
            String processName = DictUtils.getDictLabel("ic_event_process", event.geteProcess());
            icEventVo.seteProcessName(processName);
            // 获取事件信息
            IcEventInfo info = icEventInfoService.selectIcEventInfoByEId(event.geteId());
            IcEventInfoVo infoVo = new IcEventInfoVo();
            BeanUtils.copyProperties(info, infoVo);
            icEventVo.setInfo(infoVo);
            // 获取流程列表
            List<IcEventProcess> processes = icEventProcessService.selectIcEventProcessListByEId(event.geteId());
            List<IcEventProcessVo> processVos = Collections.emptyList();
            if (!CollectionUtils.isEmpty(processes)) {
                processVos = processes.stream().map(p -> {
                    IcEventProcessVo processVo = new IcEventProcessVo();
                    BeanUtils.copyProperties(p, processVo);
                    String an = DictUtils.getDictLabel("ic_event_audit", String.valueOf(p.geteAudit()));
                    processVo.setAuditName(an);
                    String pn = DictUtils.getDictLabel("ic_event_process", String.valueOf(p.geteProcess()));
                    processVo.setProcessName(pn);
                    return processVo;
                }).collect(Collectors.toList());
            }
            icEventVo.setProcesses(processVos);
            return icEventVo;
        }).collect(Collectors.toList());
        return collect;
    }
}
